package com.gzsxy.esjy.decoder;

import com.gzsxy.esjy.handler.StatusHandler;
import com.gzsxy.esjy.request.HttpServletRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 基础解码器
 *
 * @author zhangxiaolong
 * @date 2022/7/6 9:36
 * @param <T>
 */
public abstract class BaseMessageDecoder<T> extends MessageToMessageDecoder<T> {

	protected final static Logger log = LoggerFactory.getLogger(BaseMessageDecoder.class);

	private final static String HANDLER = "status-halder" ;
	
//	/**
//	 * 缓存文件清除
//	 * @param request
//	 */
//	public  final static void cleanFile(HttpServletRequest request) {
//		log.info("清除缓存遗留文件-1");
//		if(request instanceof DefaultMultipartHttpRequest) {
//			HttpPostRequestDecoder postRequestDecoder = ((DefaultMultipartHttpRequest)request).getPostRequestDecoder();
//			if(postRequestDecoder != null) {
//				//清除缓存文件
//				log.info("清除缓存遗留文件");
//				postRequestDecoder.cleanFiles();
//			}
//		}
//	}
	
	
	/**
	 * 操作request
	 * 
	 * @param ctx
	 * @param request
	 * @return
	 */
	protected HttpServletRequest request(ChannelHandlerContext ctx, HttpServletRequest request) {
		StatusHandler halder = (StatusHandler) ctx.channel().pipeline().get(HANDLER);
		if (request == null) {
			return halder.getRequest();
		}
		halder.setRequest(request);
		return request;
	}

	/**
	 * 操作request
	 * 
	 * @param ctx
	 * @return
	 */
	protected HttpServletRequest removeRequest(ChannelHandlerContext ctx) {
		StatusHandler halder = (StatusHandler) ctx.channel().pipeline().get(HANDLER);
		halder.setRequest(null);
		return null;
	}
}
